#include"manager.h"

//默认构造
Manager::Manager()
{

}

//有参构造 管理员姓名 密码
Manager::Manager(string name, string pwd)
{
	//初始化管理员信息
	this->m_Name = name;
	this->m_Pwd = pwd;

	//初始化容器 获取到所有文件中 学生、老师信息
	this->initvector();
}

//选择菜单
void Manager::operMenu()
{
	cout << "欢迎管理员：" << this->m_Name << "登录!" << endl;
	cout << "\t\t ----------------------------------\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          1.添加账号              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          2.查看账号              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          3.查看机房              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          4.清空预约              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t|          0.注销登录              |\n";
	cout << "\t\t|                                  |\n";
	cout << "\t\t ----------------------------------\n";
	cout << "请选择您的操作： " << endl;
}

//添加账号
void Manager::addPerson()
{

	cout << "请输入添加账号的类型" << endl;
	cout << "1、添加学生" << endl;
	cout << "2、添加老师" << endl;

	string fileName;  //操作文件名
	string tip;  //提示id号
	string errorTip;  //重复错误提示

	ofstream ofs;  //文件操作对象

	int select = 0;
	cin >> select;

	if (select == 1)
	{
		fileName = STUDENT_FILE;
		tip = "请输入学号：";
		errorTip = "学号重复，请重新输入";
	}
	else
	{
		fileName = TEACHER_FILE;
		tip = "请输入职工编号";
		errorTip = "职工号重复，请重新输入";
	}

	//利用追加的方式 写文件
	ofs.open(fileName, ios::out | ios::app);
	int id;
	string name;
	string pwd;
	cout << tip << endl;
	while (true)
	{
		cin >> id;
		bool ret=checkRepeater(id,select);
		if (ret)  //有重复
		{
			cout << errorTip << endl;
		}
		else
		{
			break;
		}

	}

	cout << "请输入姓名：" << endl;
	cin >> name;

	cout << "请输入密码：" << endl;
	cin >> pwd;

	ofs << id << " " << name << " "<<pwd << " " << endl;
	cout << "添加成功" << endl;

	system("pause");
	system("cls");

	ofs.close();
}

//查看账号
void Manager::showPerson()
{

}

//查看机房信息
void Manager::showComputer()
{

}

//清空预约记录
void Manager::cleanFile()
{

}

//初始化容器
void Manager::initvector()
{
	//读取学生文件中信息
	ifstream ifs;
	ifs.open(STUDENT_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "文件读取失败" << endl;
		return;
	}

	vStu.clear();
	vTea.clear();

	Student s;
	while (ifs >> s.m_Id && ifs >> s.m_Name && ifs >> s.m_Pwd)
	{
		vStu.push_back(s);
	}
	cout << "当前学生数量为：" << vStu.size() << endl;
	ifs.close();  //学生初始化

	//读取老师文件信息
	ifs.open(TEACHER_FILE, ios::in);

	Teacher t;
	while (ifs >> t.m_EmpId && ifs >> t.m_Name && ifs >> t.m_Pwd)
	{
		vTea.push_back(t);
	}
	cout << "当前教师数量为：" << vTea.size() << endl;

	ifs.close();

}

//检测重复 参数；（传入Id,传入类型） 返回值；（true代表有重复 false代表没有重复）
bool Manager::checkRepeater(int id, int type)
{
	if (type == 1)
	{
		for (const auto& it : vStu)
		{
			if (id == it.m_Id)
			{
				return true;
			}
		}
	}
	else
	{
		for (const auto& it : vTea)
		{
			if (id == it.m_EmpId)
			{
				return true;
			}
		}
	}
	return false;
}
